package com.zhb.controller;

import com.zhb.beans.AjaxResult;
import com.zhb.beans.Permission;
import com.zhb.beans.User;
import com.zhb.service.PermissionService;
import com.zhb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @program: atcrowdfunding-web
 * @description: 页面跳转
 * @author: rainboz
 * @create: 2019-09-19 12:19
 **/
@Controller
public class DispatcherController {
    @Autowired
    private UserService userService;

    @Autowired
    private PermissionService permissionService;

    /**
     * 根目录
     *
     * @return
     */
    @RequestMapping("/")
    public String index() {
        return "login";
    }

    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
//        session.removeAttribute("loginUser");
        session.invalidate();//所有数据失效
        return "redirect:login";
    }
    @RequestMapping("/error")
    public String error() {
        return "error";
    }

    /**
     * 主页面
     *
     * @return
     */
    @RequestMapping("/main")
    public String main() {
        return "main";
    }

    /**
     * 登录功能:AJAX发送请求
     *
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doAjaxLogin", method = RequestMethod.POST)
    public Object doAjaxLogin(User user, HttpSession session) {
        AjaxResult ajaxResult = new AjaxResult();
        User dbUser = userService.query4Login(user);
//        System.out.println("user:"+user);
//        System.out.println("dbUser:"+dbUser);
        if (dbUser != null) {
            session.setAttribute("loginUser", dbUser);

            //获取用户权限信息
            List<Permission> permissions = permissionService.queryPermissionByUser(dbUser);

            Map<Integer, Permission> map = new HashMap<>();
            Set<String> uriSet = new HashSet<String>();
            Permission root = null;
            for (Permission permission : permissions) {
                map.put(permission.getId(), permission);
                if ( permission.getUrl() != null && !"".equals(permission.getUrl()) ) {
                    uriSet.add(session.getServletContext().getContextPath() + permission.getUrl());
                }
            }
            session.setAttribute("authUriSet", uriSet);
            for (Permission permission : permissions) {
                Permission child = permission;
                if (child.getPid() == 0) {
                    root = permission;
                } else {
                    Permission parent = map.get(child.getPid());
                    parent.getChildren().add(child);
                }
            }

            session.setAttribute("rootPermission", root);

            ajaxResult.setSuccess(true);
        } else {
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }

    /**
     * 登录功能:from表单发送请求
     */
    @RequestMapping("/dologin")
    public String dologin(User user, Map<String, Object> map) {
        //1. HttpServletRequest
        //2. @RequestParam("username") String username,...
        //3. POJO
        User dbUser = userService.query4Login(user);
        if (dbUser != null) {
            //登陆成功
            return "main";
        } else {
            //登陆失败,回到登陆页面，并且提供错误信息
            String error_msg = "用户名或者密码错误！";
            map.put("errorMsg", error_msg);
            return "redirect:login";
        }

    }
}
